package com.zhenyi.jiahua.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jiahua.common.model.JhRole;
import com.jiahua.common.model.JhStaff;
import com.jiahua.common.model.JhStudent;
import com.zhenyi.jiahua.index.JHController;

/**
 * 校验当前学员是否自己的学员
 * 
 * @author cen
 *
 */
public class SelfClientInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation inv) {

		JHController controller = (JHController) inv.getController();
		JhStaff staff = controller.getStaff();
		Long staffId = staff.getId();

		Long studentId = controller.getParaToLong("studentId");
		if (studentId == null) {
			inv.invoke();
			return;
		}

		JhStudent student = JhStudent.dao.findById(studentId);

		if (JhRole.dao.havePermission(staffId, JhRole.ROLE_CHECK_SELF_ONLY, JhRole.TYPE_ALL)) {
			boolean isStudentSelf = false;
			if (JhStaff.TYPE_TMK.equals(staff.getType())) {
				if (staffId.equals(student.getTmkId())) {
					isStudentSelf = true;
				}
			} else if (JhStaff.TYPE_CC.equals(staff.getType())) {
				if (staffId.equals(student.getCcId())) {
					isStudentSelf = true;
				}
			} else if (JhStaff.TYPE_TUIGUANG.equals(staff.getType())) {
				if (staffId.equals(student.getTuiguangId())) {
					isStudentSelf = true;
				}
			}
			if (!isStudentSelf) {
				controller.setAttr("read", 1);
			}
		}
		inv.invoke();
	}

}
